package com.zkjl.protect_business.controller;

import com.zkjl.protect_business.common.*;
import com.zkjl.protect_business.dto.NoticeDTO;
import com.zkjl.protect_business.dto.PageBaseDTO;
import com.zkjl.protect_business.dto.PageDTO;
import com.zkjl.protect_business.dto.SortDTO;
import com.zkjl.protect_business.entity.Notice;
import com.zkjl.protect_business.entity.NoticeRecord;
import com.zkjl.protect_business.entity.User;
import com.zkjl.protect_business.repository.UserRepository;
import com.zkjl.protect_business.service.FileService;
import com.zkjl.protect_business.service.NoticeRecordService;
import com.zkjl.protect_business.service.NoticeService;
import com.zkjl.protect_business.utils.JwtUtil;
import com.zkjl.protect_business.utils.ResultUtil;
import com.zkjl.protect_business.vo.NoticeVO;
import com.zkjl.protect_business.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.rest.RestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/notices")
@Api(tags = {"文件通知"})
public class NoticeController {


    @Autowired
    NoticeService noticeService;
    @Autowired
    NoticeRecordService noticeRecordService;

    @Autowired
    FileService fileService;

    @Autowired
    JwtUtil jwtUtil;
    @Autowired
    UserRepository userRepository;


    @ApiOperation(value = "发起通知", notes = "发起通知", httpMethod = "POST")
    @PostMapping
    @SystemLog(description = "发起通知")
    public ApiResponse<Notice> save(@RequestBody @Validated NoticeDTO noticeDTO) {

        Notice save = noticeService.save(noticeDTO);
        return ApiResponse.ofSuccess(save);

    }

    @ApiOperation(value = "通知列表", notes = "通知列表", httpMethod = "GET")
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", value = "关键词" , paramType = "query"),
            @ApiImplicitParam(name = "status", value = "通知中 通知完成", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "下发开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "下发结束时间", paramType = "query"),
    })
    @SystemLog(description = "查看通知列表")
    public ApiResponse<PageVO<NoticeVO>> page(PageBaseDTO pageBaseDTO, SortDTO sortDTO, String status) {

        PageImpl page = noticeService.page(pageBaseDTO, sortDTO, status);

        return ResultUtil.successPagesVO(page);

    }

    @ApiOperation(value = "删除通知", notes = "删除通知", httpMethod = "DELETE")
    @DeleteMapping(value = "{id}")
    @SystemLog(description = "删除通知")
    public ApiResponse del(@PathVariable String id) {

        Boolean del = noticeService.del(id);
        return ApiResponse.ofSuccess(del);
    }


    @ApiOperation(value = "已读", notes = "已读", httpMethod = "POST")
    @PostMapping(value = "{noticeId}/read")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "通知id", required = true, paramType = "query")
    })
    @SystemLog(description = "确认接收文件通知")
    public ApiResponse confirm( @PathVariable String noticeId) {
        NoticeRecord confirm = noticeRecordService.confirm(noticeId);
        return ApiResponse.ofSuccess(confirm);
    }

    @ApiOperation(value = "通知下载文件", notes = "下载文件", httpMethod = "GET")
    @GetMapping(value = "{noticeId}/{fileId}")
    @SystemLog(description = "下载文件")
    public void downloadByBotice(HttpServletResponse response, @PathVariable String fileId, @PathVariable String noticeId,String token) throws IOException {

        if (StringUtils.isBlank(token)) {
            throw new BaseException(Status.BAD_REQUEST.getCode(), "token异常");
        }
        String subToken = token.substring(7);

        String username = jwtUtil.getUsernameFromJWT(subToken);
        User user = userRepository.findByUserName(username);
        //登录成功
        UserThreadLocal.set(user);//将user对象放置在本地线程中，方便controller和service获取

        noticeService.download(response, noticeId, fileId);
    }


}
